package com.blakequ.androidblemanager.utils;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OKHttpUtil {
    private static final String KEY = "mimaxiang_8J1U3C-!29end";
    //    FormBody formBody = new FormBody.Builder()
//            .add("mobile", username)
//            .add("password", password)
//            .build();
    private OkHttpClient instance;

    //1.私有化构造函数
    private OKHttpUtil() {
        //2.使用DCL模式单例模式
        if (null == instance) {
            synchronized (OKHttpUtil.class) {
                if (null == instance) {
                    instance = new OkHttpClient();
                }
            }
        }
    }

    public static OKHttpUtil getInstance() {
        OKHttpUtil instance = null;
        //2.使用DCL模式单例模式
        if (null == instance) {
            synchronized (OKHttpUtil.class) {
                if (null == instance) {
                    instance = new OKHttpUtil();
                }
            }
        }
        return instance;
    }

    //封装get请求
    public void get(String urlString,FormBody formBody, Callback callback){
        Request request = new Request.Builder().url(urlString).build();
        instance.newCall(request).enqueue(callback);
    }

    //封装post请求
    public void post(String urlString, String bodyJson, Callback callback){
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/html;charset=utf-8"), bodyJson);
//        MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
//        RequestBody requestBody = FormBody.create(mediaType, bodyJson);
        Request request = new Request.Builder().method("POST",requestBody)
                .addHeader("Content-Type","application/json")
                .url(urlString).build();
        instance.newCall(request).enqueue(callback);
    }

}


